package com.zmj.sy.mom.srv.aps.service;

import com.alibaba.spring.util.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.dto.PartType.PartTypeDto;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.PartType;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectItemResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseSelectResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.group.*;
import com.zmj.sy.mom.srv.aps.bean.vo.partType.PartTypeSelectVo;
import com.zmj.sy.mom.srv.aps.bean.vo.partType.PartTypeVo;
import com.zmj.sy.mom.srv.aps.mapper.*;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class PartTypeService {

    private final PartTypeMapper partTypeMapper;

    /**
     * @Description:  新增物料类型
     * @author: lyh
     * @Param:
     * @date: 2024/9/25 9:07
     */
    @Transactional
    public void add(PartTypeDto partTypeDto){
        PartType partType = MkBeanUtils.copyProperties(partTypeDto, PartType.class);
        partTypeMapper.insert(partType);
    }

    /**
     * @Description:  物料类型修改
     * @author: lyh
     * @Param:
     * @date: 2024/9/25 9:35
     */
    @Transactional
    public void edit(PartTypeDto partTypeDto){
        LambdaQueryWrapper<PartType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PartType::getId, partTypeDto.getId());
        wrapper.eq(PartType::getDeleted, 0);
        PartType partType = partTypeMapper.selectOne(wrapper);
        MkBeanUtils.copyPropertiesIgnore(partTypeDto, partType);
        partTypeMapper.updateById(partType);
    }

    /**
     * @Description:  物料类型删除
     * @author: lyh
     * @Param:
     * @date: 2024/9/25 9:44
     */
    @Transactional
    public void delete(Integer id){
        LambdaQueryWrapper<PartType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PartType::getId, id);
        wrapper.eq(PartType::getDeleted, 0);
        partTypeMapper.delete(wrapper);
    }



    /**
     * @Description:  物料类型列表查询
     * @author: lyh
     * @Param:
     * @date: 2024/9/25 9:26
     */
    public BasePageResVo<PartTypeDto> list(PartTypeSelectVo partTypeVo) {
        LambdaQueryWrapper<PartType> wrapper = new LambdaQueryWrapper<>();
        if(ObjectUtils.isNotEmpty(partTypeVo.getTypeCode())){
            wrapper.eq(PartType::getTypeCode, partTypeVo.getTypeCode());
        }
        wrapper.eq(PartType::getDeleted, 0);
        Page<PartType> partTypePage = partTypeMapper.selectPage(partTypeVo.toPage(), wrapper);
        BasePageResVo<PartTypeDto> resVo = BasePageResVo.of(partTypePage, PartTypeDto.class);
        return resVo;
    }


}
